<?php

class Empeval_Methods_Data_BonusPeriod {
	
	public $id;
	public $name;
	public $from;
	public $to;
	public $isModified = false;
	
	public function __construct(EmpevalBonusPeriods $bp, $userPosition=null, $year=null) {		
		$this->id = $bp->getId();
		$this->name = $bp->getName();
		$this->from = $this->getFormatedDate($bp->getPeriodFrom());
		$this->to = $this->getFormatedDate($bp->getPeriodTo());
		$this->fillDeadline = $this->getFormatedDate($bp->getFillDeadline());
		$this->updateDeadline = $this->getFormatedDate($bp->getUpdateDeadline());
		
		if ($userPosition instanceof EmpevalUserPositions && $userPosition->getDeactiveDate() != null) {
			$startPeriodDate = new DateTime($year . '-' . $bp->getPeriodFrom());
			$endPeriodDate = new DateTime($year . '-' . $bp->getPeriodTo());
			if ($startPeriodDate <= $userPosition->getDeactiveDate(null) && $endPeriodDate > $userPosition->getDeactiveDate(null)) {
				$this->to = $userPosition->getDeactiveDate('d.m');
				$this->isModified = true;
			}
		}
		if ($userPosition instanceof EmpevalUserPositions && !$userPosition->getIsFirst()) {
			$startPeriodDate = new DateTime($year . '-' . $bp->getPeriodFrom());
			$endPeriodDate = new DateTime($year . '-' . $bp->getPeriodTo());
			if ($startPeriodDate <= $userPosition->getActiveDate(null) && $endPeriodDate > $userPosition->getActiveDate(null)) {
				$this->from = $userPosition->getActiveDate('d.m');
				$this->isModified = true;
			}
		}
	}
	
	private function getFormatedDate($date) {
		$ret = new DateTime('now');
		$dateArr = explode('-', $date);
		$ret->setDate($ret->format('Y'), $dateArr[0], $dateArr[1]);
		return $ret->format('d.m');
	}
	
	
	
}